package com.xsy.android.compose.wandroid.ui.page.home

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.xsy.android.compose.wandroid.logic.base.repository.BaseArticlePagingRepository
import com.xsy.android.compose.wandroid.logic.base.viewmodel.BaseArticleViewModel
import com.xsy.android.compose.wandroid.logic.model.BannerBean
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.logic.model.Query
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
 *@author: xiaosy
 *时间: 2024/7/8
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class HomePageViewModel(app: Application) : BaseArticleViewModel(app) {
    override val repository: BaseArticlePagingRepository
        get() = HomeArticlePagingRepository()

    fun getHomeArticle() {
        searchArticle(Query())
    }

    private var bannerJob: Job? = null
    private val _bannerState = MutableLiveData<PlayState<List<BannerBean>>>()
    val bannerState: LiveData<PlayState<List<BannerBean>>>
        get() = _bannerState

    fun getBanner() {
        bannerJob?.cancel()
        bannerJob = viewModelScope.launch(Dispatchers.IO) {
            (repository as HomeArticlePagingRepository).getBanner(_bannerState)
        }
    }
}